Imports System.ComponentModel
Public Class frmTest

    Private oCMApi As CMApi.clsUtil
    Private CEConn As CMApi.clsUtil.CEConnection
    Private oIDoc As FileNet.Api.Core.IDocument

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click

        Dim ceURI As String
        Dim sLogFolder As String = "d:\CMApiLog"
        If txtLogFolder.Text <> "" Then
            sLogFolder = txtLogFolder.Text
        End If
        oCMApi = New CMApi.clsUtil(sLogFolder, "trace")
        ceURI = "http://" & txtIP.Text & ":" & txtPort.Text & "/wsi/FNCEWS40MTOM/"
        'http://192.168.4.20:7001/wsi/FNCEWS40MTOM/
        CEConn = oCMApi.LogonCE(txtUserID.Text, txtPassword.Text, ceURI)
        TabControl1.Enabled = True
        txtLogonStatus.Text = "Logon Success ; Domain Name : " & CEConn.domainName
        InitOS()
    End Sub

    Private Sub frmTest_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        TabControl1.Enabled = False
        'Add Check In Type to combo box
        cboCheckInType.Items.Add("MAJOR_VERSION")
        cboCheckInType.Items.Add("MINOR_VERSION")

        txtIP.Text = "10.229.201.47"
        txtPort.Text = "9081"
        txtUserID.Text = "p8service_tw"
        txtPassword.Text = "abc123"
        txtLogFolder.Text = "C:\CMApiLog"
        dgvProperties.AllowUserToAddRows = False

    End Sub



    Private Sub InitOS()
        cboOS.Items.Clear()
        Dim ary As ArrayList = CEConn.aryObjectStoreName
        For i As Integer = 0 To ary.Count - 1
            cboOS.Items.Add(ary(i))
        Next
    End Sub

    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged

    End Sub


    Private Sub btnCreareFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreareFolder.Click
        Dim oFolder As FileNet.Api.Core.IFolder
        Dim oProp(0) As CMApi.clsUtil.PropertyInfo
        Try
            oFolder = oCMApi.CreateFolder(cboOS.SelectedItem.ToString, txtPath.Text, txtFolderName.Text, txtClassName.Text)
            txtResult.Text = "Create Folder Success , Path Name: " & oFolder.PathName

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub

    Private Sub BtnCreateDoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCreateDoc.Click
        Try
            'Create without properties 
            oCMApi.CreateDoc(cboOS.SelectedItem, txtDocClassName.Text, txtFileName.Text, True, _
                             CMApi.clsUtil.MimeType.image_tiff, txtDocTitle.Text, _
                             IIf(chkFileIn.Checked = True, True, False), txtFileInFolderName.Text, True, FileNet.Api.Constants.CheckinType.MAJOR_VERSION)
            'Create with properties 
            'Dim oProps(2) As CMApi.clsUtil.PropertyInfo
            'oProps(0).PropertyName = "APPLNO"
            'oProps(0).PropertyValue = Now.ToString("hhmmss")

            'oProps(1).PropertyName = "BATCHNO"
            'oProps(1).PropertyValue = Now.ToString("yyyyMMdd")

            'oProps(2).PropertyName = "Formid"
            'oProps(2).PropertyValue = Now.ToString("yyyyMM")

            'oCMApi.CreateDoc(cboOS.SelectedItem, txtDocClassName.Text, txtFileName.Text, True, _
            '         CMApi.clsUtil.MimeType.image_bmp, txtDocTitle.Text, _
            '         False, txtFileInFolderName.Text, oProps, True, FileNet.Api.Constants.CheckinType.MAJOR_VERSION)
            MessageBox.Show("OK")
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try




    End Sub

    Private Sub btnGetDocs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetDocs.Click
        Try
            QryDocList()
            MessageBox.Show("OK")
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub QryDocList()
        'Dim dt As DataTable = oCMApi.GetDocList(cboOS.SelectedItem, "select id, applno,p0,p1,p2,p3 from GenericDSIMS  ")
        Dim dt As DataTable = oCMApi.GetDocList(cboOS.SelectedItem, txtSearchSQL.Text)
        dgvProperties.DataSource = Nothing
        dgvProperties.DataSource = dt
        Dim o As New LogHandler.clsLogger
        For Each dc As DataColumn In dt.Columns
            o.writeLine("c:\", "Column Name : " & dc.ColumnName & " Data Type : " & dc.DataType.ToString)
        Next
    End Sub
    Private Sub btnGetIDoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetIDoc.Click
        Try
            oIDoc = oCMApi.GetIDocument(cboOS.SelectedItem, txtDocID.Text)
            Me.btnGetDocWithIDoc.Enabled = True
            Me.btnGetDocWithOsName.Enabled = True
            Me.btnGetDocWithIOS.Enabled = True

            MessageBox.Show("OK")

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub

    Private Sub btnGetDocWithIDoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetDocWithIDoc.Click
        txtFilePath.Text = oCMApi.GetDocContentByIDoc(oIDoc)
        MessageBox.Show("OK")

    End Sub

    Private Sub tpProcessDocs_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles tpProcessDocs.Enter
        If dgvProperties.SelectedRows.Count > 0 Then
            txtDocID.Text = dgvProperties.SelectedRows(0).Cells(0).Value
            txtFilePath.Text = ""
            oIDoc = Nothing
            Me.btnGetDocWithIDoc.Enabled = False
            Me.btnGetDocWithOsName.Enabled = False
            Me.btnGetDocWithIOS.Enabled = False
        End If
    End Sub

    Private Sub btnGetDocWithIOS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetDocWithIOS.Click
        Dim os As FileNet.Api.Core.IObjectStore
        os = oCMApi.ObjectStore(cboOS.SelectedItem)
        txtFilePath.Text = oCMApi.GetDocContentByIOS(os, txtDocID.Text)
        MessageBox.Show("OK")

    End Sub

    Private Sub btnGetDocWithOsName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetDocWithOsName.Click
        txtFilePath.Text = oCMApi.GetDocContentByOSName(cboOS.SelectedItem, txtDocID.Text)
        MessageBox.Show("OK")
    End Sub

    Private Sub btnDelDoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelDoc.Click
        If MessageBox.Show("Delete Current Document", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            oCMApi.DeleteDoc(oIDoc)
            QryDocList()
            MessageBox.Show("OK")
        End If
    End Sub

    Private Sub btnUpdProperty_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdProperty.Click
        Dim oProps(2) As CMApi.clsUtil.PropertyInfo
        oProps(0).PropertyName = "APPLNO"
        oProps(0).PropertyValue = Now.ToString("hhmmss")

        oProps(1).PropertyName = "BATCHNO"
        oProps(1).PropertyValue = Now.ToString("yyyyMMdd")

        oProps(2).PropertyName = "Formid"
        oProps(2).PropertyValue = Now.ToString("yyyyMM")
        oCMApi.setDocProperties(oIDoc, oProps)
    End Sub

    Private Sub btnCheckFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckFolder.Click
        oCMApi.CheckFolderExist(cboOS.SelectedItem.ToString, txtPath.Text)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' dgvProperties.SelectedRows(0).Cells(0).Value
        'Force select field : id,applno,totalpages
        Try
            Dim sSQL As String = txtSearchSQL.Text
            Dim idx As Integer = sSQL.IndexOf("from")
            txtSearchSQL.Text = "select id,applno,totalpages from " & sSQL.Substring(idx + 4)
            QryDocList()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim oProp(0) As CMApi.clsUtil.PropertyInfo
        Dim docid As String = ""
        Dim oDoc As FileNet.Api.Core.IDocument = Nothing
        Try
            With oProp(0)
                .PropertyName = "TotalPages"
                .PropertyValue = 1
            End With

            For i As Integer = 0 To dgvProperties.Rows.Count - 1
                If dgvProperties.Rows(i).Cells(2).Value = 0 Then
                    docid = dgvProperties.Rows(i).Cells(0).Value
                    oDoc = oCMApi.GetIDocument(cboOS.SelectedItem, docid)
                    oCMApi.SetDocProperties(oDoc, oProp)
                End If
                Application.DoEvents()
                If i Mod 1000 = 0 Then
                    Dim s As String = ""
                End If
            Next
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim docid As String = ""
        Dim oDoc As FileNet.Api.Core.IDocument = Nothing
        If MessageBox.Show("Confirm Delete ", "", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.Cancel Then
            Exit Sub
        End If
        While True
            Dim dt As DataTable = oCMApi.GetDocList(cboOS.SelectedItem, txtSearchSQL.Text)
            If dt Is Nothing OrElse dt.Rows.Count = 0 Then
                Exit Sub
            End If
            dgvProperties.DataSource = Nothing
            dgvProperties.DataSource = dt

            For i As Integer = 0 To dgvProperties.Rows.Count - 1
                docid = dgvProperties.Rows(i).Cells(0).Value
                oDoc = oCMApi.GetIDocument(cboOS.SelectedItem, docid)
                oCMApi.DeleteDoc(oDoc)
                Application.DoEvents()
            Next
        End While

    End Sub
End Class
